package com.atguigu.schedule.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 *
 * 操作数据库的工具类
 * @author atguigu
 * @version 1.0
 * 2024-11-19
 */
public class JDBCUtils {

    public static Connection getConnection() throws Exception {
        Properties properties = new Properties();
//        ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");这样就找不到
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(is);
        DataSource ds = DruidDataSourceFactory.createDataSource(properties);
        return ds.getConnection();
    }

    public static void closeResource(Connection conn, Statement statement, ResultSet rs){
//        try {
//            DbUtils.close(conn);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        try {
//            DbUtils.close(statement);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        try {
//            DbUtils.close(rs);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(statement);
        DbUtils.closeQuietly(rs);
    }
}
